package com.paas.form.service.impl;


import com.paas.common.jdbc.model.DbConfig;
import com.paas.common.model.DataBaseEntity;
import com.paas.common.util.BeanUtil;
import com.paas.common.util.StringUtil;
import com.paas.common.web.SystemConst;
import com.paas.form.cache.AttributeCache;
import com.paas.form.cache.ChildViewCache;
import com.paas.form.cache.IDataSourceCache;
import com.paas.form.cache.ViewCache;
import com.paas.form.entity.*;
import com.paas.form.queue.FormApilogProducter;
import com.paas.form.service.*;
import com.paas.plus.tool.form.model.*;
import com.paas.plus.tool.form.server.FormService;
import com.publics.entity.UcUserEntity;
import com.publics.feign.service.IUcDataBaseService;
import com.publics.feign.service.IUcUserService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;


public class FormServiceImpl  implements FormService {

    @Autowired
    public IUcUserService ucUserService;

    @Autowired
    public IFormViewService formViewService;
    //定版视图
    @Autowired
    public IFormViewPubService formViewPubService;

    @Autowired
    private IDataSourceCache dataSourceCache;

    @Autowired
    private IFormDataChangeInfoService formDataChangeInfoService;

    @Autowired
    private IFormDataChangeInfoDetailService formDataChangeInfoDetailService;

    //定版属性
    @Autowired
    public IFormAttributePubService formAttributePubService;

    @Autowired
    public ChildViewCache childViewCache;

    @Autowired
    public ViewCache viewCache;

    @Autowired
    public AttributeCache attributeCache;

    @Autowired
    public FormApilogProducter formApiProducter;

    @Override
    public View getViewById(String id) {
        View view = new View();
        FormViewPubEntity bean = viewCache.getViewById(id);
        transformModel(view,bean);
        return view;
    }

    @Override
    public List<View> getChildViewById(String masterViewId) {
        List<View> views=new ArrayList<>();
        //查看是否有子视图，有则进行添加
        FormViewPubEntity childFormViewEntity =new FormViewPubEntity();
        List<FormViewPubEntity> list = childViewCache.getChildViewByViewId(masterViewId);
        if(BeanUtil.isNotEmpty(list)){
            for (int i = 0; i <list.size() ; i++) {
                View childview = new View();
                transformModel(childview,list.get(i));
                //设置视图类型1：主视图；2：子视图
                childview.setViewType("2");
                views.add(childview);
            }
        }
        return views;
    }

    private void transformModel(View view,FormViewPubEntity bean){
        view.setCode(bean.getCode());
        view.setName(bean.getName());
        view.setType("tile".equals(bean.getType())?"1":"2");
        view.setLinkTable(bean.getLinkTable());
        view.setDatasourceId(bean.getDatasource());
        view.setIdField(bean.getIdField());
        view.setParentField(bean.getParentField());
        view.setViewField(bean.getViewField());
        view.setParentId(bean.getParentId());
        view.setForeignKeyMapping(bean.getForeignKeyMapping());
        view.setContainerField(bean.getContainerField());
        //设置视图类型1：主视图；2：子视图
        if(StringUtil.isNotEmpty(bean.getViewId())){
            view.setViewType("1");
        }else {
            view.setViewType("2");
        }
        view.setId(bean.getId());
        view.setStatusField(bean.getStatusField());
    }

    @Override
    public List<Attribute> getAttributeByViewIds(List<String> viewIds) {
        List<FormAttributePubEntity> formAttributeEntityList = attributeCache.getListByViewIds(viewIds);
        List<Attribute> attributeList = new ArrayList<>();
        if(BeanUtil.isNotEmpty(formAttributeEntityList)){
            Attribute attribute = null;
            for(FormAttributePubEntity temp : formAttributeEntityList){
                attribute = new Attribute();
                BeanUtil.copyProperties(attribute,temp);
                attributeList.add(attribute);
            }
        }
        return attributeList;
    }

    @Override
    public DbConfig getViewByDatasourceId(String datasourceId) {
        return dataSourceCache.queryJdbcById(datasourceId);
    }

    @Override
    public DataChangeInfo getDataChangeInfoById(String id){
        DataChangeInfo dataChangeInfo=new DataChangeInfo();
        FormDataChangeInfoEntity formDataChangeInfoEntity=formDataChangeInfoService.getById(id);
        dataChangeInfo.setId(formDataChangeInfoEntity.getId());
        dataChangeInfo.setViewFormId(formDataChangeInfoEntity.getViewFormId());
        dataChangeInfo.setApprovalStatus(formDataChangeInfoEntity.getApprovalStatus());
        dataChangeInfo.setType(formDataChangeInfoEntity.getType());
        dataChangeInfo.setStatus(formDataChangeInfoEntity.getStatus());
        dataChangeInfo.setViewDataId(formDataChangeInfoEntity.getViewDataId());
        dataChangeInfo.setMasterViewEditDataId(formDataChangeInfoEntity.getMasterViewEditDataId());
        return dataChangeInfo;

    }

    @Override
    public List<DataChangeInfoDetail> getDetailsByChangeInfoId(String changeInfoId){
        List<DataChangeInfoDetail> dataChangeInfoDetails=new ArrayList<>();
        FormDataChangeInfoDetailEntity formDataChangeInfoDetailEntity=new FormDataChangeInfoDetailEntity();
        formDataChangeInfoDetailEntity.setChangeInfoId(changeInfoId);
        formDataChangeInfoDetailEntity.setStatus(SystemConst.NORMAL_STATUS);
        List<FormDataChangeInfoDetailEntity> formDataChangeInfoDetailEntityList=formDataChangeInfoDetailService.getList(formDataChangeInfoDetailEntity);
        if(formDataChangeInfoDetailEntityList.size()>0){
            for (FormDataChangeInfoDetailEntity formDetail:formDataChangeInfoDetailEntityList) {
                DataChangeInfoDetail dataChangeInfoDetail=new DataChangeInfoDetail();
                dataChangeInfoDetail.setType(formDetail.getType());
                dataChangeInfoDetail.setStatus(formDetail.getStatus());
                dataChangeInfoDetail.setChangeInfoId(formDetail.getChangeInfoId());
                dataChangeInfoDetail.setLinkTable(formDetail.getLinkTable());
                dataChangeInfoDetail.setPrimaryKeyField(formDetail.getPrimaryKeyField());
                dataChangeInfoDetail.setPrimaryKeyValue(formDetail.getPrimaryKeyValue());
                dataChangeInfoDetail.setViewId(formDetail.getViewId());
                dataChangeInfoDetail.setAttributeName(formDetail.getAttributeName());
                dataChangeInfoDetail.setAttributeValue(formDetail.getAttributeValue());
                dataChangeInfoDetail.setUpdateValue(formDetail.getUpdateValue());
                dataChangeInfoDetails.add(dataChangeInfoDetail);
            }
        }

        return dataChangeInfoDetails;
    }

    @Override
    public void saveApiLog(ApiLog apiLog) {
        formApiProducter.saveApiLog(apiLog);
    }

    @Override
    public ShowUserItem getUserById(String userId) {
        if(StringUtil.isEmpty(userId)){
            return null;
        }
        UcUserEntity ucUserEntity = ucUserService.queryById(userId);
        if(BeanUtil.isEmpty(ucUserEntity)){
            return null;
        }
        ShowUserItem showUserItem = new ShowUserItem();
        showUserItem.setUserId(ucUserEntity.getId());
        showUserItem.setUserName(ucUserEntity.getUserName());
        return showUserItem;
    }

    /*--------------------------------------------------未使用-----------------------------------------------------------*/
    @Override
    public List<View> getViewListById(String masterViewId){
        List<View> views=new ArrayList<>();
        View masterview = new View();
        FormViewPubEntity bean = formViewPubService.getById(masterViewId);
        transformModel(masterview,bean);
        //设置视图类型1：主视图；2：子视图
        masterview.setViewType("1");

        //判断主键是否是编码值
        //查询视图的属性
        FormAttributePubEntity attribute = new FormAttributePubEntity();
        attribute.setStatus(SystemConst.NORMAL_STATUS);
        attribute.setCode(bean.getIdField());
        attribute.setViewId(masterViewId);
        List<FormAttributePubEntity> attributeList = formAttributePubService.getList(attribute);
        if(attributeList.size()>0){
            if(StringUtil.isNotEmpty(attributeList.get(0).getReferenceType())&& "codeRule".equals(attributeList.get(0).getReferenceType())&&StringUtil.isNotEmpty(attributeList.get(0).getReferenceData())){
                masterview.setKeyIsCodec("1");
            }else {
                masterview.setKeyIsCodec("2");
            }
        }
        //放入主视图
        views.add(masterview);
        //查看是否有子视图，有则进行添加
        FormViewPubEntity childFormViewEntity =new FormViewPubEntity();
        childFormViewEntity.setViewId(bean.getId());
        childFormViewEntity.setStatus(SystemConst.NORMAL_STATUS);
        List<FormViewPubEntity> list = formViewPubService.getList(childFormViewEntity);
        for (int i = 0; i <list.size() ; i++) {
            View childview = new View();
            transformModel(childview,list.get(i));
            //设置视图类型1：主视图；2：子视图
            childview.setViewType("2");

            //判断主键是否是编码值
            //查询视图的属性
            FormAttributePubEntity childAttribute = new FormAttributePubEntity();
            childAttribute.setStatus(SystemConst.NORMAL_STATUS);
            childAttribute.setCode(bean.getIdField());
            childAttribute.setViewId(list.get(i).getId());
            List<FormAttributePubEntity> childAttributeList = formAttributePubService.getList(childAttribute);
            if(BeanUtil.isNotEmpty(attributeList) && attributeList.size()>0){
                if(StringUtil.isNotEmpty(childAttributeList.get(0).getReferenceType())&& "codeRule".equals(childAttributeList.get(0).getReferenceType())&&StringUtil.isNotEmpty(childAttributeList.get(0).getReferenceData())){
                    childview.setKeyIsCodec("1");
                }else {
                    childview.setKeyIsCodec("2");
                }
            }
            views.add(childview);
        }
        return views;
    }

    @Override
    public List<Attribute> getAttributeByViewId(String viewId) {
        List<String> viewIds = new ArrayList<>();
        viewIds.add(viewId);
        return getAttributeByViewIds(viewIds);
    }

}